﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SerialPortHelperLib;
using System.IO.Ports;

namespace XChipTool
{
    public partial class XChip_AEState : DevExpress.XtraEditors.XtraForm
    {
        public SerialPort sPortTemp { get; set; }



        // 定义一个静态变量来保存类的实例
        private static XChip_AEState _xChip_AEState;
        // 定义一个标识确保线程同步
        private static readonly object locker = new object();
        // 定义私有构造函数，使外界不能创建该类实例

        private bool IsUsbOpen = false;
        //记录是否使用I2C设备读写
        public bool m_open = false;
        public XChip_AEState()
        {
            InitializeComponent();
        }

        public XChip_AEState(bool isUsbOpen = false)
        {
            IsUsbOpen = isUsbOpen;
            InitializeComponent();
        }
        public static XChip_AEState GetInstance(bool isUsbOpen)
        {
            // 当第一个线程运行到这里时，此时会对locker对象 "加锁"，
            // 当第二个线程运行该方法时，首先检测到locker对象为"加锁"状态，该线程就会挂起等待第一个线程解锁
            // lock语句运行完之后（即线程运行完之后）会对该对象"解锁"
            // 双重锁定只需要一句判断就可以了
            if (_xChip_AEState == null || _xChip_AEState.IsDisposed)
            {
                lock (locker)
                {
                    // 如果类的实例不存在则创建，否则直接返回
                    if (_xChip_AEState == null || _xChip_AEState.IsDisposed)
                    {
                        _xChip_AEState = new XChip_AEState(isUsbOpen);
                    }
                }
            }
            return _xChip_AEState;
        }

        public static XChip_AEState GetInstance()
        {
            // 当第一个线程运行到这里时，此时会对locker对象 "加锁"，
            // 当第二个线程运行该方法时，首先检测到locker对象为"加锁"状态，该线程就会挂起等待第一个线程解锁
            // lock语句运行完之后（即线程运行完之后）会对该对象"解锁"
            // 双重锁定只需要一句判断就可以了
            if (_xChip_AEState == null || _xChip_AEState.IsDisposed)
            {
                lock (locker)
                {
                    // 如果类的实例不存在则创建，否则直接返回
                    if (_xChip_AEState == null || _xChip_AEState.IsDisposed)
                    {
                        _xChip_AEState = new XChip_AEState();
                    }
                }
            }
            return _xChip_AEState;
        }
        private void btn_RefreshAeState_Click(object sender, EventArgs e)
        {
            if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            switch (SerialSend.ReadRegister((Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == "Scene_Mode".ToLower()).First().Address, 16)), sPortTemp) & 0x3)
            {
                case 0:
                    txt_CurScene.Text = "Day";
                    break;
                case 1:
                    txt_CurScene.Text = "Night";
                    break;
                case 2:
                    txt_CurScene.Text = "Midnight";
                    break;
                default:
                    break;
            }
            long nHighest, nHigh, nMid, nLow, nValue;
            nHigh = SerialSend.ReadRegister((Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == "AE_AVRBIRGHT_h".ToLower()).First().Address, 16)), sPortTemp);
            nLow = SerialSend.ReadRegister((Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == "AE_AVRBIRGHT_L".ToLower()).First().Address, 16)), sPortTemp);

            nValue = (nHigh << 8) | nLow;
            AE_AVRBIRGHT.Text = SerialSend.ReadRegister(0x80300053, sPortTemp).ToString("X2");

            nHighest = SerialSend.ReadRegister((Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == "AE_EDIT_EXPLINE_h".ToLower()).First().Address, 16)), sPortTemp);
            nHigh = SerialSend.ReadRegister((Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == "AE_EDIT_EXPLINE_m0".ToLower()).First().Address, 16)), sPortTemp);
            nMid = SerialSend.ReadRegister((Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == "AE_EDIT_EXPLINE_m1".ToLower()).First().Address, 16)), sPortTemp);
            nLow = SerialSend.ReadRegister((Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == "AE_EDIT_EXPLINE_l".ToLower()).First().Address, 16)), sPortTemp);

            nValue = (nHighest << 20) | (nHigh << 12) | (nMid << 4) | (nLow >> 4);
            AE_EDIT_EXPLINE.Text = nValue.ToString("X2");


            nHighest = SerialSend.ReadRegister((Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == "AE_SENSORGAIN_h".ToLower()).First().Address, 16)), sPortTemp);
            nHigh = SerialSend.ReadRegister((Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == "AE_SENSORGAIN_m0".ToLower()).First().Address, 16)), sPortTemp);
            nMid = SerialSend.ReadRegister((Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == "AE_SENSORGAIN_m1".ToLower()).First().Address, 16)), sPortTemp);
            nLow = SerialSend.ReadRegister((Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == "AE_SENSORGAIN_l".ToLower()).First().Address, 16)), sPortTemp);

            nValue = (nHighest << 24) | (nHigh << 16) | (nMid << 8) | nLow;
            AE_SENSORGAIN.Text = ((float)nValue / 0x20).ToString("0.00");

            //if (AE_ISP_GAINENABLE_EN.CheckState == CheckState.Checked)
            if (((SerialSend.ReadRegister((Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == "Isp_Gain_en".ToLower()).First().Address, 16)), sPortTemp)) >> 5 & 0x01) == 1)
            {
                nHigh = SerialSend.ReadRegister((Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == "AE_EDIT_ISPGAIN_h".ToLower()).First().Address, 16)), sPortTemp);
                nLow = SerialSend.ReadRegister((Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == "AE_EDIT_ISPGAIN_l".ToLower()).First().Address, 16)), sPortTemp);

                nValue = (nHigh << 8) | nLow;
                AE_EDIT_ISPGAIN.Text = ((float)nValue / 0x4).ToString("0.00");
            }
            else
            {
                AE_EDIT_ISPGAIN.Text = "1.0";
            }

            nHighest = SerialSend.ReadRegister((Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == "AE_EDIT_GAIN_h".ToLower()).First().Address, 16)), sPortTemp);
            nHigh = SerialSend.ReadRegister((Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == "AE_EDIT_GAIN_m0".ToLower()).First().Address, 16)), sPortTemp);
            nMid = SerialSend.ReadRegister((Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == "AE_EDIT_GAIN_m1".ToLower()).First().Address, 16)), sPortTemp);
            nLow = SerialSend.ReadRegister((Convert.ToUInt32(XChipHelp.XChipAe.Where(c => c.NameType.ToLower() == "AE_EDIT_GAIN_l".ToLower()).First().Address, 16)), sPortTemp);

            nValue = (nHighest << 24) | (nHigh << 16) | (nMid << 8) | nLow;
            AE_EDIT_GAIN.Text = ((float)nValue / 0x20).ToString("0.00");
        }
    }
}